const app = getApp();
import QR from '/../../../utils/wxqrcode.js';
var md5 = require("../../../utils/md5.js");
Page({
  data: {
    aboutUsTitle: '',
    aboutUsContent: '',
    servicePhoneNumber: '',
    balance: 0,
    freeze: 0,
    score: 0,
    score_sign_continuous: 0,
    iconSize: 45,
    iconColor: '#999999',
    width: wx.getStorageSync('width'),
    height: wx.getStorageSync('height'),
    qrcode: '',
    is_show_open: true
  },
  onShow: function (option) {
    let that = this

    that.getlod();
    that.getUserInfo();
    that.getUserAmount();

    var scene = app.globalData.scene;
    var is_show_open = that.is_show_open;
    if (scene == 1036 || scene == 1069) {
        is_show_open = false
    } else {
      is_show_open = true
    }
    wx.getSystemInfo({
      //获取系统信息成功，将系统窗口的宽高赋给页面的宽高  
      success: function (res) {
        that.setData({
          width: res.windowWidth,
          height: res.windowHeight
        })
      }
    })
    that.setData({
      open_id: app.globalData.openId,
      user_id: app.globalData.userId,
      is_show_open: is_show_open,
    })
  },

  aboutUs: function() {
    var that = this
    wx.showModal({
      title: wx.getStorageSync('mallName'),
      content: wx.getStorageSync('shoptese'),
      showCancel: false
    })
  },
  makePhoneCall: function() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: wx.getStorageSync('shopTel'),
      success: function(res) {},
      fail: function(res) {
        wx.showModal({
          title: '呼叫失败',
          content: '请稍后再试',
          showCancel: false,
        })
      },
      complete: function(res) {},
    })
  },
  getUserInfo: function() {
    var that = this;
    var open_id = app.globalData.openId;
    var service = 'User.GetInfo';
    var user_id = app.globalData.userId;
    var sign = md5.md5(open_id + service + user_id);
    wx.request({
      url: app.globalData.serviceUrl + 'User.GetInfo',
      data: {
        open_id: open_id,
        sign: sign,
        user_id: user_id,
      },
      success: function(res) {
        var result = res.data.data;
        if (result.code == 0) {
          console.log(result.info[0].user_avatar)
          that.setData({
            avatar: result.info[0].user_avatar,
            nickname: result.info[0].nickname,
          })
        }
      },
    })

  },
  getUserAmount: function() {
    var that = this;
    var open_id = app.globalData.openId;
    var service = 'User.GetMyAccount';
    var user_id = app.globalData.userId;
    var sign = md5.md5(open_id + service + user_id);
    wx.request({
      url: app.globalData.serviceUrl + 'User.GetMyAccount',
      data: {
        open_id: open_id,
        sign: sign,
        user_id: user_id,
      },
      success: function(res) {
        var result = res.data.data;
        if (result.code == 0) {
          that.setData({
            balance: result.info[0].account,
            freeze: 0,
            score: 0
          });
          app.globalData.userAccount = result.info[0].account;
        }else{
          that.setData({
            balance: 0,
            freeze: 0,
            score: 0
          });
        }
      },
      complete: function(res) {
        console.log(res);
      },
    })

  },
  scoresign: function() {
    var that = this;
    wx.showToast({
      title: '签到成功',
      icon: 'success',
      duration: 2000
    })

  },
  relogin: function() {
    wx.navigateTo({
      url: "/pages/authorize/index"
    })
    this.onLoad()
  },
  recharge: function() {
    wx.navigateTo({
      url: "/pages/recharge/index"
    })
  },
  withdraw: function() {
    wx.navigateTo({
      url: "/pages/withdraw/index"
    })
  },
  Cancellation: function() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否注销当前账号',
      success: function(res) {
        if (res.confirm) {
          var service = 'User.CheckLand';
          var open_id = app.globalData.openId;
          var user_id = app.globalData.userId;
          var sign = md5.md5(open_id + service + user_id);
          wx.request({
            url: app.globalData.serviceUrl + service,
            header: {
              'Content-Type': 'json'
            },
            data: {
              sign: sign,
              user_id: user_id,
              open_id: open_id,
            },
            success: (res) => {
              var result = res.data.data;
              if (result.code == 0) {
                if (result.info == 1) {
                  that.editLand();
                } else {
                  app.globalData.userTel = '';
                  app.globalData.userId = '';
                  app.globalData.openId = '';
                  app.globalData.pageFlag = false;
                  wx.clearStorage();
                  wx.navigateTo({
                    url: "/pages/login/index"
                  })
                }
              } else {
                app.globalData.userTel = '';
                app.globalData.userId = '';
                app.globalData.openId = '';
                app.globalData.pageFlag = false;
                wx.clearStorage();
                wx.navigateTo({
                  url: "/pages/login/index"
                })
              }
            }
          })
        }
      }
    })
  },
  editLand: function() {
    var service = 'User.EditLand';
    var open_id = app.globalData.openId;
    var user_id = app.globalData.userId;
    var pid = 0;
    var sign = md5.md5(open_id + pid + service + user_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        pid: pid,
      },
      success: (res) => {
        var result = res.data.data;

        if (result.code == 0) {
          if (result.info == 0) {
            app.globalData.userTel = '';
            app.globalData.userId = '';
            app.globalData.openId = '';
            app.globalData.pageFlag = false;
            wx.clearStorage();
            wx.navigateTo({
              url: "/pages/login/index"
            })
          }
        } else {
          wx.showToast({
            title: '修改失败',
            icon: 'success',
            duration: 2000
          })
        }
      }
    })
  },

  getlod: function() {

    var that = this;
    var service = 'User.CheckLand';
    var open_id = app.globalData.openId;
    var user_id = app.globalData.userId;
    var sign = md5.md5(open_id + service + user_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 0) {
            app.globalData.userTel = '';
            app.globalData.userId = '';
            app.globalData.openId = '';
            app.globalData.pageFlag = false;
            wx.clearStorage();
            wx.navigateTo({
              url: "/pages/login/index"
            })
          }
        } else {
          app.globalData.userTel = '';
          app.globalData.userId = '';
          app.globalData.openId = '';
          app.globalData.pageFlag = false;
          wx.clearStorage();
          wx.navigateTo({
            url: "/pages/login/index"
          })
        }
      }
    })
  },
  //错误提示
  launchAppError: function (e) {
    console.log(e.detail.errMsg)
    var errMsg = e.detail.errMsg;
    if (errMsg == 'invalid scene') {
      wx.showToast({
        title: "跳转错误",
        icon: 'none',
        duration: 2000
      })
    }
  } 
})